﻿/*
 * usart.c
 *
 * Created: 2013-12-23 12:48:52
 *  Author: Michał
 */ 

// Includes -------------------------------------------------------------------------------------------
#include "usart.h"



/******************************************************************************************************
Function: USART_Init(unsigned int speed)
Description: This function initializes UART interface  
 *****************************************************************************************************/ 
void USART_Init(void)
{
	UBRRH = (unsigned char)(USART_SPEED >> 8);				//Set baud rate 
	UBRRL = (unsigned char)(USART_SPEED);
	
	UCSRB = (1<<RXEN) | (1<<TXEN);					//Enable receiver and transmitter 
	
	//Access to register UCSRC (URSEL=0 - access to UBRRH)
	UCSRC |= (1<<URSEL ) | (1<<USBS) | (1<<UCSZ1) | (1<<UCSZ0);	//Set 2 stop bits, set 8 bits character size 		
	UCSRB |= (1<<RXCIE);							//Enable receive complete interrupt 
}
/*** END OF FUNCTION USART_Init ***********************************************************************
******************************************************************************************************/




/******************************************************************************************************
Function: USART_Send(uint8_t data)
Description: This function sends character by USART interface  
 *****************************************************************************************************/ 
void USART_Send(uint8_t data)
{
	while (!( UCSRA & (1<<UDRE)));					//Wait for empty transmit buffer
	UDR = data;										//Put data into buffer and sends the data 
}
/*** END OF FUNCTION USART_Send ***********************************************************************
******************************************************************************************************/




/******************************************************************************************************
Function: USART_Print(char *tab)
Description: This function sends string by USART interface
 *****************************************************************************************************/ 
void USART_Print(char *tab)
{
	int i=0;

	for(i=0; i<strlen(tab); i++)					//Until char != '/0'
	{
		while (!( UCSRA & (1<<UDRE)));				//Wait for empty transmit buffer
		UDR = tab[i];								//Put data into buffer and sends the data
	}
}
/*** END OF FUNCTION USART_Print **********************************************************************
******************************************************************************************************/




/******************************************************************************************************
Function: uint8_t USART_Receive();
Description: This function received character from USART
 *****************************************************************************************************/ 
uint8_t USART_Receive(void)
{
	while (!(UCSRA & (1<<RXC)));					//Wait for data to be received 
	return UDR;										//Return received data
}
/*** END OF FUNCTION USART_Receive() ******************************************************************
******************************************************************************************************/